package targaman.moltenwow.autovoter.components;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;
import targaman.moltenwow.autovoter.Logger.MoltenLogger;
import targaman.moltenwow.autovoter.lang.Constants;
import targaman.moltenwow.autovoter.services.MoltenWoWVoter;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jonathan on 29/12/2014.
 */
@Component(value = Constants.MOLTEN_WOW_VOTER)
public class MoltenWoWVoterImpl implements MoltenWoWVoter {


    private MoltenWoWVoterImpl http;

    private List<String> cookies;
    private HttpURLConnection conn;

    public static final String WWW_MOLTEN_WOW_COM = "https://www.molten-wow.com";
    String loginURL = "https://www.molten-wow.com/login/";
    String accountURL = "https://www.molten-wow.com/account/";
    String accountVoteURL = "https://www.molten-wow.com/account/vote/";
    String votePostPart1 = "https://www.molten-wow.com/account/pg/0:0:0:d:0/";
    String votePostPart2 = "https://www.molten-wow.com/account/pg/0:0:0:v:undefined/";

    public static final String ALREADY_VOTED = "Already Voted! Site#";
    String voteSite1 = "http://www.openwow.com/vote=6";
    String voteSite1_out = "http://www.openwow.com/visit=6";
    public static final String WWW_MOLTEN_WOW_COM_ACCOUNT_0_0_0_v_3 =  "https://www.molten-wow.com/account/pg/0:0:0:v:3/";

    String voteSite2 = "http://www.xtremetop100.com/in.php?site=1132296123";
    String voteSite2_out = "http://www.xtremetop100.com/out.php?site=1132296123";
    public static final String WWW_MOLTEN_WOW_COM_ACCOUNT_4_0_0_1_0_2 = "https://www.molten-wow.com/account/#4:0:0:1:0:2";

    public static final String HTTPS_WWW_MOLTEN_WOW_COM_ACCOUNT_PG_4_0_0_D_0 = "https://www.molten-wow.com/account/pg/4:0:0:d:0/";


    @Override
    public boolean checkIfVoted(HttpURLConnection conn, int voteId) {
        http = new MoltenWoWVoterImpl();
        this.conn = conn;
        String page = null;

        try {
            page = http.GetPageContent(accountURL);

            String postParams = http.getFormParams(page);
            http.sendPost(votePostPart1, postParams);
            http.sendPost(votePostPart2, postParams);
            page = http.GetPageContent(votePostPart2);
            postParams = http.getFormParams(page, voteId);
            if (!postParams.equals(ALREADY_VOTED)) {
                MoltenLogger.Log("You haven't voted yet! Site #" + voteId);
            return false;
            } else {
                MoltenLogger.Log(ALREADY_VOTED+ voteId);
                return true;
            }
        }
            catch (Exception d)
            {
                MoltenLogger.Log(d.getStackTrace().toString());
                return true;
            }
    }

    @Override
    public boolean vote(HttpURLConnection conn, int voteId) {
        http = new MoltenWoWVoterImpl();
        this.conn = conn;
        String page = null;

        try {
            page = http.GetPageContent(accountURL);

            String postParams = http.getFormParams(page);
            http.sendPost(votePostPart1, postParams);
            http.sendPost(votePostPart2, postParams);
            page = http.GetPageContent(votePostPart2);
            postParams = http.getFormParams(page, voteId);
            if(!postParams.equals(ALREADY_VOTED)) {


                if(postParams.contains("id=1"))
                {
                    http.sendPost(accountVoteURL, postParams);
                    page = http.GetPageContent(voteSite1);
                    page = http.GetPageContent(voteSite1_out);
                    page = http.GetPageContent(WWW_MOLTEN_WOW_COM);
                    page = http.GetPageContent(WWW_MOLTEN_WOW_COM_ACCOUNT_0_0_0_v_3);
                    page = http.GetPageContent(accountURL);
                    http.sendPost(HTTPS_WWW_MOLTEN_WOW_COM_ACCOUNT_PG_4_0_0_D_0, postParams);
                    page = http.GetPageContent(accountURL);


                    URL obj = new URL(voteSite1_out);
                    conn = (HttpURLConnection) obj.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setRequestProperty("Host", "www.openwow.com");
                    conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
                    conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                    conn.setRequestProperty("Accept-Language", "en-GB,en;q=0.5");
                    conn.setRequestProperty("Referer", "http://www.openwow.com/listing=world+of+warcraft&cat=mists+of+pandaria");
                    if (cookies != null) {
                        for (String cookie : this.cookies) {
                            conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
                        }
                    }
                    conn.setRequestProperty("Connection", "keep-alive");
                    conn.connect();
                    int responseCode = conn.getResponseCode();
                    System.out.println("\nSending 'GET' request to URL : " + voteSite1_out);
                    System.out.println("Response Code : " + responseCode);

                    obj = new URL(WWW_MOLTEN_WOW_COM);
                    conn = (HttpURLConnection) obj.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setRequestProperty("Host", "www.molten-wow.com");
                    conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
                    conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                    conn.setRequestProperty("Accept-Language", "en-GB,en;q=0.5");
                    conn.setRequestProperty("Referer", voteSite1_out);
                    if (cookies != null) {
                        for (String cookie : this.cookies) {
                            conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
                        }
                    }
                    conn.setRequestProperty("Connection", "keep-alive");
                    conn.connect();
                    responseCode = conn.getResponseCode();
                    System.out.println("\nSending 'GET' request to URL : " + voteSite1_out);
                    System.out.println("Response Code : " + responseCode);

                    obj = new URL(WWW_MOLTEN_WOW_COM_ACCOUNT_0_0_0_v_3);
                    conn = (HttpURLConnection) obj.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setRequestProperty("Host", "www.molten-wow.com");
                    conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
                    conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                    conn.setRequestProperty("Accept-Language", "en-GB,en;q=0.5");
                    conn.setRequestProperty("Referer", voteSite1_out);
                    if (cookies != null) {
                        for (String cookie : this.cookies) {
                            conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
                        }
                    }
                    conn.setRequestProperty("Connection", "keep-alive");
                    conn.connect();
                    responseCode = conn.getResponseCode();
                    System.out.println("\nSending 'GET' request to URL : " + voteSite1_out);
                    System.out.println("Response Code : " + responseCode);

                    obj = new URL(accountURL);
                    conn = (HttpURLConnection) obj.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setRequestProperty("Host", "www.molten-wow.com");
                    conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
                    conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                    conn.setRequestProperty("Accept-Language", "en-GB,en;q=0.5");
                    conn.setRequestProperty("Referer", voteSite1_out);
                    if (cookies != null) {
                        for (String cookie : this.cookies) {
                            conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
                        }
                    }
                    conn.setRequestProperty("Connection", "keep-alive");
                    conn.connect();
                    responseCode = conn.getResponseCode();
                    System.out.println("\nSending 'GET' request to URL : " + voteSite1_out);
                    System.out.println("Response Code : " + responseCode);

                    obj = new URL(HTTPS_WWW_MOLTEN_WOW_COM_ACCOUNT_PG_4_0_0_D_0);
                    conn = (HttpURLConnection) obj.openConnection();
                    conn.setRequestMethod("POST");
                    conn.setRequestProperty("Host", "www.molten-wow.com");
                    conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
                    conn.setRequestProperty("Accept", "*/*");
                    conn.setRequestProperty("Accept-Language", "en-GB,en;q=0.5");
                    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                    conn.setRequestProperty("X-Requested-With", "XMLHttpRequest");
                    conn.setRequestProperty("Referer", voteSite1_out);
                    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                    conn.setRequestProperty("Content-Length", "65");
                    if (cookies != null) {
                        for (String cookie : this.cookies) {
                            conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
                        }
                    }
                    conn.setRequestProperty("Connection", "keep-alive");
                    conn.setRequestProperty("Pragma", "no-cache");
                    conn.setRequestProperty("Cache-Control", "no-cache");
                    conn.setDoOutput(true);
                    conn.setDoInput(true);

                    DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
                    wr.writeBytes(postParams);
                    wr.flush();
                    wr.close();

                    responseCode = conn.getResponseCode();
                    System.out.println("\nSending 'POST' request to URL : " + HTTPS_WWW_MOLTEN_WOW_COM_ACCOUNT_PG_4_0_0_D_0);
                    System.out.println("Post parameters : " + postParams);
                    System.out.println("Response Code : " + responseCode);







                    System.out.println("Done");
                    MoltenLogger.Log("Successfully Voted #1");
                    return true;
                }
                if(postParams.contains("id=2"))
                {
                    http.sendPost(accountVoteURL, postParams);
                    page = http.GetPageContent(voteSite2);
                    page = http.GetPageContent(voteSite2_out);
                    page = http.GetPageContent(WWW_MOLTEN_WOW_COM);
                    page = http.GetPageContent(WWW_MOLTEN_WOW_COM_ACCOUNT_4_0_0_1_0_2);
                    page = http.GetPageContent(accountURL);
                    http.sendPost(HTTPS_WWW_MOLTEN_WOW_COM_ACCOUNT_PG_4_0_0_D_0, postParams);
                    page = http.GetPageContent(accountURL);


                    URL obj = new URL(voteSite2_out);
                    conn = (HttpURLConnection) obj.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setRequestProperty("Host", "www.xtremetop100.com");
                    conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
                    conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                    conn.setRequestProperty("Accept-Language", "en-GB,en;q=0.5");
                    conn.setRequestProperty("Referer", "http://www.xtremetop100.com/world-of-warcraft");
                    if (cookies != null) {
                        for (String cookie : this.cookies) {
                            conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
                        }
                    }
                    conn.setRequestProperty("Connection", "keep-alive");
                    conn.connect();
                    int responseCode = conn.getResponseCode();
                    System.out.println("\nSending 'GET' request to URL : " + voteSite2_out);
                    System.out.println("Response Code : " + responseCode);

                    obj = new URL(WWW_MOLTEN_WOW_COM);
                    conn = (HttpURLConnection) obj.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setRequestProperty("Host", "www.molten-wow.com");
                    conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
                    conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                    conn.setRequestProperty("Accept-Language", "en-GB,en;q=0.5");
                    conn.setRequestProperty("Referer", voteSite2_out);
                    if (cookies != null) {
                        for (String cookie : this.cookies) {
                            conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
                        }
                    }
                    conn.setRequestProperty("Connection", "keep-alive");
                    conn.connect();
                    responseCode = conn.getResponseCode();
                    System.out.println("\nSending 'GET' request to URL : " + voteSite2_out);
                    System.out.println("Response Code : " + responseCode);

                    obj = new URL(WWW_MOLTEN_WOW_COM+ "/account/#4:0:0:1:0:2");
                    conn = (HttpURLConnection) obj.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setRequestProperty("Host", "www.molten-wow.com");
                    conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
                    conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                    conn.setRequestProperty("Accept-Language", "en-GB,en;q=0.5");
                    conn.setRequestProperty("Referer", voteSite2_out);
                    if (cookies != null) {
                        for (String cookie : this.cookies) {
                            conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
                        }
                    }
                    conn.setRequestProperty("Connection", "keep-alive");
                    conn.connect();
                    responseCode = conn.getResponseCode();
                    System.out.println("\nSending 'GET' request to URL : " + voteSite2_out);
                    System.out.println("Response Code : " + responseCode);

                    obj = new URL(accountURL);
                    conn = (HttpURLConnection) obj.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setRequestProperty("Host", "www.molten-wow.com");
                    conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
                    conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                    conn.setRequestProperty("Accept-Language", "en-GB,en;q=0.5");
                    conn.setRequestProperty("Referer", voteSite2_out);
                    if (cookies != null) {
                        for (String cookie : this.cookies) {
                            conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
                        }
                    }
                    conn.setRequestProperty("Connection", "keep-alive");
                    conn.connect();
                    responseCode = conn.getResponseCode();
                    System.out.println("\nSending 'GET' request to URL : " + voteSite2_out);
                    System.out.println("Response Code : " + responseCode);

                    obj = new URL(HTTPS_WWW_MOLTEN_WOW_COM_ACCOUNT_PG_4_0_0_D_0);
                    conn = (HttpURLConnection) obj.openConnection();
                    conn.setRequestMethod("POST");
                    conn.setRequestProperty("Host", "www.molten-wow.com");
                    conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
                    conn.setRequestProperty("Accept", "*/*");
                    conn.setRequestProperty("Accept-Language", "en-GB,en;q=0.5");
                    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                    conn.setRequestProperty("X-Requested-With", "XMLHttpRequest");
                    conn.setRequestProperty("Referer", voteSite2_out);
                    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                    conn.setRequestProperty("Content-Length", "65");
                    if (cookies != null) {
                        for (String cookie : this.cookies) {
                            conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
                        }
                    }
                    conn.setRequestProperty("Connection", "keep-alive");
                    conn.setRequestProperty("Pragma", "no-cache");
                    conn.setRequestProperty("Cache-Control", "no-cache");
                    conn.setDoOutput(true);
                    conn.setDoInput(true);

                    DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
                    wr.writeBytes(postParams);
                    wr.flush();
                    wr.close();

                    responseCode = conn.getResponseCode();
                    System.out.println("\nSending 'POST' request to URL : " + HTTPS_WWW_MOLTEN_WOW_COM_ACCOUNT_PG_4_0_0_D_0);
                    System.out.println("Post parameters : " + postParams);
                    System.out.println("Response Code : " + responseCode);







                    System.out.println("Done");
                    MoltenLogger.Log("Successfully Voted #2");
                    return true;
                }
                else {
                    MoltenLogger.Log("Invalid Vote Site Id!");
                    return false;
                }
            }
            else {
                MoltenLogger.Log(ALREADY_VOTED + voteId);
                return false;
            }

        } catch (Exception e) {
            e.printStackTrace();
            MoltenLogger.Log(e.getStackTrace().toString());
            return false;
        }

    }



    private void sendPost(String url, String postParams) throws Exception {

        URL obj = new URL(url);

        conn = (HttpURLConnection) obj.openConnection();

        // Acts like a browser
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Host", "www.molten-wow.com");
        conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
        conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        conn.setRequestProperty("Accept-Language","en-GB,en;q=0.5");
        conn.setRequestProperty("Accept-Encoding","gzip, deflate");
        conn.setRequestProperty("Referer","https://www.molten-wow.com/login/");
//        conn.setRequestProperty("Referer","https://www.molten-wow.com/");
        if (cookies != null) {
            for (String cookie : this.cookies) {
                conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
            }
        }
        conn.setRequestProperty("Connection", "keep-alive");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length", Integer.toString(postParams.length()));
//        conn.setRequestProperty("Host", "molten-wow.com");
//        conn.setRequestProperty("User-Agent", USER_AGENT);
//        conn.setRequestProperty("Accept",
//                "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
//        conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

//        conn.setRequestProperty("Connection", "keep-alive");
//        conn.setRequestProperty("Referer", "https://www.molten-wow.com/login/");
//        conn.setRequestProperty("Content-Type", "text/html");
//        conn.setRequestProperty("Content-Length", Integer.toString(postParams.length()));

        conn.setDoOutput(true);
        conn.setDoInput(true);

        // Send post request
        DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
        wr.writeBytes(postParams);
        wr.flush();
        wr.close();

        int responseCode = conn.getResponseCode();
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Post parameters : " + postParams);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in =
                new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        // System.out.println(response.toString());

    }

    private String GetPageContent(String url) throws Exception {

        URL obj = new URL(url);
        conn = (HttpURLConnection) obj.openConnection();

        // default is GET
        conn.setRequestMethod("GET");
        conn.setUseCaches(false);
        conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
        conn.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        conn.setRequestProperty("Accept-Language","en-GB,en;q=0.5");
//        conn.setRequestProperty("Accept-Encoding","gzip, deflate");
        conn.setRequestProperty("Referer","https://www.molten-wow.com/login/");

        if (cookies != null) {
            for (String cookie : this.cookies) {
                conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
            }
        }
        conn.setRequestProperty("Connection","keep-alive");
        // act like a browser
//        conn.setRequestProperty("User-Agent", USER_AGENT);
//        conn.setRequestProperty("Accept",
//                "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
//        conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
//        if (cookies != null) {
//            for (String cookie : this.cookies) {
//                conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
//            }
//        }
        int responseCode = conn.getResponseCode();
        System.out.println("\nSending 'GET' request to URL : " + url);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in =
                new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        // Get the response cookies
        setCookies(conn.getHeaderFields().get("Set-Cookie"));

        return response.toString();

    }

    private String getFormParams(String html, String username, String password)
            throws UnsupportedEncodingException {

        System.out.println("Extracting form's data...");

        Document doc = Jsoup.parse(html);

        // Google form id
        Element loginform = doc.getElementById("loginForm");
        Elements loginform2 = doc.getAllElements();
        Elements inputElements = loginform.getElementsByTag("input");
        Element userId = loginform2.get(9);
        Element passwordInput = loginform2.get(16);
        List<String> paramList = new ArrayList<String>();
        for (Element inputElement : loginform2) {
            String key = inputElement.attr("name");
            String value = inputElement.attr("value");

            if (key.equals("userID"))
                value = username;
            else if (key.equals("userPW"))
                value = password;
            paramList.add(key + "=" + URLEncoder.encode(value, "UTF-8"));
        }

        // build parameters list
        StringBuilder result = new StringBuilder();
        for (String param : paramList) {
            if (result.length() == 0) {
                result.append(param);
            } else {
                result.append("&" + param);
            }
        }
        return result.toString();
    }

    private String getFormParams(String html, int voteId)
            throws UnsupportedEncodingException {

        System.out.println("Extracting form's data...");

        Document doc = Jsoup.parse(html);

        // Google form id
//        Element loginform = doc.getElementById("loginForm");
        Elements loginform2 = doc.getAllElements();
//        Elements inputElements = loginform.getElementsByTag("input");
        Element userId = loginform2.get(9);
        Element passwordInput = loginform2.get(16);
        List<String> paramList = new ArrayList<String>();
        if(voteId == 1) {
            Element votesite1token = null;
            Element votesite1 = null;
            for(int i = 0; i < loginform2.size(); i++)
            {
                if(loginform2.get(i).toString().equals("<input type=\"hidden\" name=\"id\" value=\"" + voteId+  "\" />"))
                {
                    votesite1token = loginform2.get(i-1);
                    votesite1 = loginform2.get(i);
                    break;
                }
            }
            if(votesite1token == null || votesite1== null)
            {
                return ALREADY_VOTED;
            }
            String keyToken = votesite1token.attr("name");
            String valueToken = votesite1token.attr("value");
            String key = votesite1.attr("name");
            String value = votesite1.attr("value");
            paramList.add(keyToken + "=" + URLEncoder.encode(valueToken, "UTF-8"));
            paramList.add(key + "=" + URLEncoder.encode(value, "UTF-8"));
        }
        else if(voteId == 2) {
            Element votesite1token = null;
            Element votesite1 = null;
            for(int i = 0; i < loginform2.size(); i++)
            {
                if(loginform2.get(i).toString().equals("<input type=\"hidden\" name=\"id\" value=\"" + voteId + "\" />"))
                {
                    votesite1token = loginform2.get(i-1);
                    votesite1 = loginform2.get(i);
                    break;
                }
            }
            if(votesite1token == null || votesite1== null)
            {
                return ALREADY_VOTED;
            }
            //prevent getting this far if already voted!
            String keyToken = votesite1token.attr("name");
            String valueToken = votesite1token.attr("value");
            String key = votesite1.attr("name");
            String value = votesite1.attr("value");
            paramList.add(keyToken + "=" + URLEncoder.encode(valueToken, "UTF-8"));
            paramList.add(key + "=" + URLEncoder.encode(value, "UTF-8"));
        }


        // build parameters list
        StringBuilder result = new StringBuilder();
        for (String param : paramList) {
            if (result.length() == 0) {
                result.append(param);
            } else {
                result.append("&" + param);
            }
        }
        return result.toString();
    }

    private String getFormParams(String html)
            throws UnsupportedEncodingException {

        System.out.println("Extracting form's data...");

        Document doc = Jsoup.parse(html);

        // Google form id
//        Element loginform = doc.getElementById("loginForm");
        Elements loginform2 = doc.getAllElements();
//        Elements inputElements = loginform.getElementsByTag("input");
        Element userId = loginform2.get(9);
        Element passwordInput = loginform2.get(16);
        List<String> paramList = new ArrayList<String>();
        for (Element inputElement : loginform2) {
            String key = inputElement.attr("name");
            String value = inputElement.attr("value");

            if (key.equals("_mToken"))
                MoltenLogger.Log("Found token!");
            else if (key.equals("id"))
                MoltenLogger.Log("Found id!");
            if(!key.equals("") && !value.equals("")) {
                paramList.add(key + "=" + URLEncoder.encode(value, "UTF-8"));
            }
        }

        // build parameters list
        StringBuilder result = new StringBuilder();
        for (String param : paramList) {
            if (result.length() == 0) {
                result.append(param);
            } else {
                result.append("&" + param);
            }
        }
        return result.toString();
    }

    public List<String> getCookies() {
        return cookies;
    }

    private void setCookies(List<String> cookies) {
        this.cookies = cookies;
    }



}
